package com.lemon.smartsafety.ui.activity.fire.item;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.http.HttpBack;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.BaseMode;
import com.lemon.smartsafety.result.fire.BuildingTypeScore;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.adapter.fire.NumberProgressBarAdapter;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.lemon.smartsafety.view.ViewFireCompanyNone;
import com.lemon.smartsafety.view.list.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class FirefightingClassifyStatisticsActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.lineAll)
    LinearLayout lineAll;
    @Bind(R.id.listViewOne)
    MyListView listViewOne;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.viewFireCompanyNone)
    ViewFireCompanyNone viewFireCompanyNone;


    private NumberProgressBarAdapter numberProgressBarAdapter;
    private List<BuildingTypeScore> mList;
    private List<BuildingTypeScore> buildingTypeScoreList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_firefighting_classify_statistics;
    }

    @Override
    protected void initTitleView() {
        setBarTitle("行业分类统计");
    }

    @Override
    protected void initView() {
        textView.setText("行业单位综合评分统计");

        mList = new ArrayList<>();
        buildingTypeScoreList = new ArrayList<>();

        numberProgressBarAdapter = new NumberProgressBarAdapter(mActivity, mList);
        listViewOne.setAdapter(numberProgressBarAdapter);


    }

    @Override
    protected void initHttp() {
        initListHttp();
    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {
        if (!CommonUtil.listIsEmpty(buildingTypeScoreList)) {
            mList.addAll(buildingTypeScoreList);
            numberProgressBarAdapter.notifyDataSetChanged();
        }
        errorView("");

    }

    /**
     * 错误的情况下 刷新
     */
    private void errorView(String msg){
        try {
            if (CommonUtil.listIsEmpty(mList)) {
                viewFireCompanyNone.setVisibility(View.VISIBLE);
                listViewOne.setVisibility(View.GONE);
                viewFireCompanyNone.setContent(msg);
                viewFireCompanyNone.setSearch("重试");
                viewFireCompanyNone.setAllClick(new ViewFireCompanyNone.AllClick() {
                    @Override
                    public void click() {
                        LogUtils.LOGE("点击了重试");
                        initHttp();
                    }
                });
            } else {
                viewFireCompanyNone.setVisibility(View.GONE);
                listViewOne.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void callFinish() {

        mActivity.finish();
    }


    public void initListHttp() {
        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");

        showLoading("");
        HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Get_building_type_score_), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                    if (!CommonUtil.objIsEmpty(baseMode)) {
                        if (HttpBack.httpBackError(baseMode)) {

                            try {
                                buildingTypeScoreList = mGson.fromJson(mGson.toJson(baseMode.getResult()), new TypeToken<List<BuildingTypeScore>>() {
                                }.getType());
                                refreshView();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {
                            ToastUtil.show(baseMode.getMsg());
                            errorView(baseMode.getMsg());
                        }
                    }
                }else{
                    buildingTypeScoreList=null;
                    errorView("网络连接失败");
                }
            }
        });

    }


}
